package server.messageContent;

import common.Constants;
import common.DeserializationManager;
import common.data.TransferFundArgs;
import common.interfaces.MessageContent;

public class TransferFundMsgContent extends TransferFundArgs implements MessageContent{
	private byte[] data;
	private int length;
	
	public TransferFundMsgContent(byte[] data){
		this.data = data;
		this.length = data.length;
		
		int currentIndex = 0;
		byte[] senderAccIdLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, senderAccIdLengthStream, 0, Constants.INT_LENGTH);
		int senderAccIdLength = DeserializationManager.getCurrent().toInt(senderAccIdLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] senderAccId = new byte[senderAccIdLength];
		System.arraycopy(data, currentIndex, senderAccId, 0, senderAccIdLength);
		this.senderAccId = DeserializationManager.getCurrent().toLong(senderAccId);
		currentIndex += senderAccIdLength;
		
		byte[] senderNameLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, senderNameLengthStream, 0, Constants.INT_LENGTH);
		int senderNameLength = DeserializationManager.getCurrent().toInt(senderNameLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] senderNameStream = new byte[senderNameLength];
		System.arraycopy(data, currentIndex, senderNameStream, 0, senderNameLength);
		this.senderName = DeserializationManager.getCurrent().toString(senderNameStream);
		currentIndex += senderNameLength;
		
		byte[] passwordLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, passwordLengthStream, 0, Constants.INT_LENGTH);
		int passwordLength = DeserializationManager.getCurrent().toInt(passwordLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] passwordStream = new byte[passwordLength];
		System.arraycopy(data, currentIndex, passwordStream, 0, passwordLength);
		this.senderPassword = DeserializationManager.getCurrent().toString(passwordStream);
		currentIndex += passwordLength;
		
		byte[] recipientAccIdLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, recipientAccIdLengthStream, 0, Constants.INT_LENGTH);
		int recipientAccIdLength = DeserializationManager.getCurrent().toInt(recipientAccIdLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] recipientAccIdStream = new byte[recipientAccIdLength];
		System.arraycopy(data, currentIndex, recipientAccIdStream, 0, senderAccIdLength);
		this.recipientAccId = DeserializationManager.getCurrent().toLong(recipientAccIdStream);
		currentIndex += recipientAccIdLength;
		
		byte[] recipientNameLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, recipientNameLengthStream, 0, Constants.INT_LENGTH);
		int recipientNameLength = DeserializationManager.getCurrent().toInt(recipientNameLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] recipientNameStream = new byte[recipientNameLength];
		System.arraycopy(data, currentIndex, recipientNameStream, 0, recipientNameLength);
		this.recipientName = DeserializationManager.getCurrent().toString(recipientNameStream);
		currentIndex += recipientNameLength;	
		
		byte[] amountLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, amountLengthStream, 0, Constants.INT_LENGTH);
		int amountLength = DeserializationManager.getCurrent().toInt(amountLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] amountStream = new byte[amountLength];
		System.arraycopy(data, currentIndex, amountStream, 0, amountLength);
		this.amount = DeserializationManager.getCurrent().toFloat(amountStream);
		currentIndex += amountLength;	
		
		byte[] currencyLengthStream = new byte[Constants.INT_LENGTH];
		System.arraycopy(data, currentIndex, currencyLengthStream, 0, Constants.INT_LENGTH);
		int currencyLength = DeserializationManager.getCurrent().toInt(currencyLengthStream);
		currentIndex += Constants.INT_LENGTH;
		
		byte[] currencyStream = new byte[currencyLength];
		System.arraycopy(data, currentIndex, currencyStream, 0, currencyLength);
		this.currency = DeserializationManager.getCurrent().toCurrencyEnum(currencyStream);
		currentIndex += currencyStream.length;
	
	}
	@Override
	public byte[] getByte() {
		// TODO Auto-generated method stub
		return this.data;
	}

	@Override
	public int getLength() {
		// TODO Auto-generated method stub
		return this.length;
	}
}
